package com.example.dao;


import com.example.entity.Guitar;
import com.example.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GuitarDao {
    
    public void add(Guitar guitar) {
        String sql = "INSERT INTO guitar(brand,type,manufacturer,price) VALUES(?,?,?,?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, guitar.getBrand());
            pstmt.setString(2, guitar.getType());
            pstmt.setString(3, guitar.getManufacturer());
            pstmt.setDouble(4, guitar.getPrice());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void update(Guitar guitar) {
        String sql = "UPDATE guitar SET brand=?, type=?, manufacturer=?, price=? WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, guitar.getBrand());
            pstmt.setString(2, guitar.getType());
            pstmt.setString(3, guitar.getManufacturer());
            pstmt.setDouble(4, guitar.getPrice());
            pstmt.setInt(5, guitar.getId());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(int id) {
        String sql = "DELETE FROM guitar WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Guitar findById(int id) {
        String sql = "SELECT * FROM guitar WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return extractGuitarFromResultSet(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Guitar> findAll() {
        List<Guitar> guitars = new ArrayList<>();
        String sql = "SELECT * FROM guitar";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                guitars.add(extractGuitarFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return guitars;
    }

    private Guitar extractGuitarFromResultSet(ResultSet rs) throws SQLException {
        Guitar guitar = new Guitar();
        guitar.setId(rs.getInt("id"));
        guitar.setBrand(rs.getString("brand"));
        guitar.setType(rs.getString("type"));
        guitar.setManufacturer(rs.getString("manufacturer"));
        guitar.setPrice(rs.getDouble("price"));
        return guitar;
    }
} 